package cn.wildfirechat.app.jpa;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource()
public interface UserPointsRepository extends CrudRepository<UserPoints, Long> {

    UserPoints findByUid(String uid);

    @Query(value = "select count(1) from user_points where uid = ?1 and available_points >= ?2", nativeQuery = true)
    int findByUidAndPoints(String userId, Integer points);

    @Query(value = "update user_points set available_points = available_points + ?2 and total_points = total_points + ?2 where uid = ?1", nativeQuery = true)
    void updateAddUserPoints(String userId, int points);

    @Query(value = "UPDATE user_points SET available_points = available_points - ?2, total_points = total_points - ?2 WHERE uid = ?1 AND available_points >= ?2", nativeQuery = true)
    int updateSubUserPoints(String userId, int points);
}
